// file      : inverse/driver.cxx
// copyright : not copyrighted - public domain

#include <memory>   // std::auto_ptr
#include <iostream>

#include <odb/database.hxx>
#include <odb/session.hxx>
#include <odb/transaction.hxx>

#include "database.hxx" // create_database

#include "../testInverse/employee.hxx"
#include "../testInverse/employee-odb.hxx"

using namespace std;
using namespace odb::core;


int main(int argc, char *argv[]) {
	using tr1::shared_ptr;

	try {
		auto_ptr<database> db(create_database(argc, argv));

		{
			{
				shared_ptr<NoteTree> er(new NoteTree("Simple Tech Ltd"));


				shared_ptr<Note> john(new Note("John", "Doe", er));
				shared_ptr<Note> jane(new Note("Jane", "Doe", er));

				er->notes().push_back(john);
				er->notes().push_back(jane);


				transaction t(db->begin());

				db->persist(er);


				db->persist(john);
				db->persist(jane);

				t.commit();
			}
		}
		{
//			session s;
			transaction t(db->begin());

			shared_ptr<NoteTree> stl(db->load<NoteTree>(1));

			Notes& es(stl->notes());
			cout << "employer name:" << stl->name() << endl;
			cout << "Size:" << stl->notes().size() << endl;
			for (Notes::iterator i(es.begin()); i != es.end(); ++i) {
				lazy_weak_ptr<Note>& lwp(*i);
				shared_ptr<Note> p(lwp.load()); // Load and lock.
				cout << "	employee first:" << p->first() << endl;
			}

			t.commit();
		}

	}
	catch (const odb::exception& e) {
		cerr << e.what() << endl;
		return 1;
	}
}
